import time

from appium.webdriver.common.appiumby import AppiumBy
from appium.webdriver.webdriver import WebDriver

from ...constants.common_sequence import SWITCH_ENV
from ...page.order_page.append_from_pre_order_page import AppendFromPreOrderPage

"""
Description:
活动页面
"""


class ActivityPage(object):

    def __init__(self, client):
        self.client = client
        self.driver: WebDriver = client.driver

    """
    Service Function
    """

    def time_sleep(self, number: int):
        time.sleep(number)
        return self

    def switch_activity(self, name):
        """
        切换活动（根据名称）
        Params:
        -name:活动名称
        """
        count = 0
        while True:
            if count > 5:
                break
            time.sleep(2)
            main_container = self.driver.find_element(AppiumBy.XPATH, f"//android.widget.FrameLayout/*[@resource-id='{SWITCH_ENV}:id/recycler_view']")
            item_main_records = main_container.find_elements(AppiumBy.ID, f'{SWITCH_ENV}:id/item_main_view')
            for record in item_main_records:
                # 重新刷新元素
                tv_name_elements = record.find_elements(AppiumBy.ID, f'{SWITCH_ENV}:id/tv_name')
                if len(tv_name_elements)==0:
                    continue
                if tv_name_elements[0].text == name:
                    record.click()
                    count=5 # 强行终止循环
                    break
                else:
                    continue
            # 页面移动
            screen_size = self.driver.get_window_size()
            width = screen_size['width']
            height = screen_size['height']
            self.driver.swipe(start_x=int(width / 2), start_y=int(height / 2),
                          end_x=int(width / 2),
                          end_y=int(height / 2) - 70, duration=50)
            count += 1
        return self

    def switch_activity_first(self):
        """
        切换活动（只选取第一个活动用于切换）
        """
        main_container = self.driver.find_element(AppiumBy.XPATH, f"//android.widget.FrameLayout/*[@resource-id='{SWITCH_ENV}:id/recycler_view']")
        item_main_records = main_container.find_elements(AppiumBy.ID, f'{SWITCH_ENV}:id/item_main_view')
        for record in item_main_records:
            enabled_state = record.get_attribute('enabled')
            if enabled_state == 'true':
                record.click()
                break
        return self

    """
    Button Function
    """

    def confirm_button(self):
        """
        确认按钮
        """
        self.driver.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/tv_confirm').click()
        from ...page.order_page.order_page import OrderPage
        return OrderPage(self.client)

    def confirm_button_for_append_page(self):
        """
        确认按钮
        """
        self.driver.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/tv_confirm').click()
        return AppendFromPreOrderPage(self.client)

    def cancel_button(self):
        """
        取消按钮
        """
        self.driver.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/tv_cancel').click()
        from ...page.order_page.order_page import OrderPage
        return OrderPage(self.client)

    def cancel_button_for_append_page(self):
        """
        取消按钮
        """
        self.driver.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/tv_cancel').click()
        return AppendFromPreOrderPage(self.client)
